package com.cxyd.modules.wms.controller;

import com.cxyd.common.exception.ErrorCode;
import com.cxyd.common.exception.RootException;
import com.cxyd.common.page.PageData;
import com.cxyd.common.utils.ProcResult;
import com.cxyd.common.utils.Result;
import com.cxyd.common.validator.group.DefaultGroup;
import com.cxyd.common.annotation.LogOperation;
import com.cxyd.common.validator.AssertUtils;
import com.cxyd.common.validator.ValidatorUtils;
import com.cxyd.common.validator.group.AddGroup;
import com.cxyd.common.validator.group.UpdateGroup;
import com.cxyd.modules.security.user.SecurityUser;
import com.cxyd.modules.security.user.UserDetail;
import com.cxyd.modules.wms.dto.TaskDTO;
import com.cxyd.modules.wms.service.TaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * 业务记录
 *
 * @author g_Cycle gengruyi01@163.com
 * @since 1.0.0 2023-04-27
 */
@RestController
@RequestMapping("wms/task")
public class TaskController {
    private final TaskService taskService;

    public TaskController(TaskService taskService) {
        this.taskService = taskService;
    }

    @GetMapping("page")
    @RequiresPermissions("wms:task:page")
    public Result<PageData<TaskDTO>> page(@RequestParam Map<String, Object> params) {
        PageData<TaskDTO> page = taskService.page(params);

        return new Result<PageData<TaskDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @RequiresPermissions("wms:task:info")
    public Result<TaskDTO> get(@PathVariable("id") Long id) {
        TaskDTO data = taskService.get(id);

        return new Result<TaskDTO>().ok(data);
    }

    @PostMapping
    @LogOperation("保存")
    @RequiresPermissions("wms:task:save")
    public Result save(@RequestBody TaskDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        taskService.save(dto);

        return new Result();
    }

    @PutMapping
    @LogOperation("修改")
    @RequiresPermissions("wms:task:update")
    public Result update(@RequestBody TaskDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        if (dto.getTaskSt() == 2) {
            throw new RootException("已完成的任务不可修改！");
        }
        taskService.updateQty(dto);

        return new Result();
    }

    @DeleteMapping
    @LogOperation("删除")
    @RequiresPermissions("wms:task:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        taskService.delete(ids);

        return new Result();
    }

    @PostMapping("request-out")
    @LogOperation("出库请求")
    @RequiresPermissions("wms:task:out")
    public Result requestOut(@RequestBody Map<String, Object> params) {
        ProcResult procResult = taskService.requestContOut(params);
        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }

    @PostMapping("request-in")
    @LogOperation("入库请求")
    // @RequiresPermissions("wms:task:in")
    public Result requestIn(@RequestBody Map<String, Object> params) {
        ProcResult procResult = taskService.requestContIn(params);
        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }
}